﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using AMS.UI.Controls;
using AMS.DataAccess.Trademark.Interfaces;
using AMS.DataAccess.Trademark.BO;
using AMS.ComponentModel;

namespace AMS.Trademark.Management.Components
{
    /// <summary>
    /// 案件详细信息
    /// </summary>
    public partial class ucRecvFileDetail : BaseComponent, IDetailControl
    {
        /// <summary>
        /// 案件详细信息
        /// </summary>
        public ucRecvFileDetail()
        {
            InitializeComponent();
            Init();
        }

        /// <summary>
        /// 案件编号
        /// </summary>
        public string RFIndex { get; private set; }
        /// <summary>
        /// 标题
        /// </summary>
        public string Title
        {
            get { return string.Format("收文信息 - {0}", RFIndex); }
        }

        private void Init()
        {
            cmbSource.ShowSubKey = false;
        }

        /// <summary>
        /// 数据绑定
        /// </summary>
        /// <param name="dataSource"></param>
        public void DataBind(DataRow dataSource)
        {
            txtRFIndex.Text = (string)dataSource["RFIndex"];
            txtRFName.Text = (string)dataSource["RFName"];
            txtAddDay.Value = (int)dataSource["AddDay"];
            txtAddMonth.Value = (int)dataSource["AddMon"];
            chkChangeStatus.Checked = Convert.ToBoolean(dataSource["isChangeCaseStatus"]);
            chkIsNeedAnswer.Checked = Convert.ToBoolean(dataSource["isNeedAnswer"]);
            chkNeedAppInfo.Checked = Convert.ToBoolean(dataSource["isNeedApplyInfo"]);
            cmbSource.Select((string)dataSource["Source"]);
            cmbCaseStatus.Select(dataSource["NewCaseStatus"].ToString());

            int dateType = (int)dataSource["DateType"];
            radioByRecvDate.Checked = (dateType == 0);
            radioBySendDate.Checked = (dateType == 1);
        }

        /// <summary>
        /// 数据绑定
        /// </summary>
        /// <param name="rfIndex"></param>
        public object DataBind(string rfIndex)
        {
            RFIndex = rfIndex;
            txtRFIndex.Text = rfIndex;
            IRFTaskManager rfManager = ObjectUtility.GetObject<IRFTaskManager>("RFTask");
            RFCategoryInfo rf = rfManager.GetRecvFile(rfIndex);
            if (rf != null)
            {
                txtAddDay.Value = rf.AddDay;
                txtAddMonth.Value = rf.AddMon;
                txtRFIndex.Text = rfIndex;
                txtRFName.Text = rf.RFName;
                chkChangeStatus.Checked = rf.isChangeCaseStatus;
                chkIsNeedAnswer.Checked = rf.isNeedAnswer;
                chkNeedAppInfo.Checked = rf.isNeedApplyInfo;
                cmbCaseStatus.Select(rf.NewCaseStatus.ToString());
                cmbSource.Select(rf.Source);
                radioByRecvDate.Checked = (rf.DateType == 0);
                radioBySendDate.Checked = (rf.DateType == 1);
            }

            return rf;
        }

        /// <summary>
        /// 设置为新建状态
        /// </summary>
        public void SetNewStatus()
        {
            SetEditable();
        }

        /// <summary>
        /// 设置为编辑状态
        /// </summary>
        public void SetEditStatus()
        {
            SetEditable();
            txtRFIndex.ReadOnly = true;
        }

        public bool CheckData()
        {
            if (txtRFIndex.Text.Trim() == "")
            {
                Balloon.balloon("请输入收文编号", txtRFIndex);
                return false;
            }
            if (txtRFName.Text.Trim() == "")
            {
                Balloon.balloon("请输入收文名称", txtRFName);
                return false;
            }
            if (cmbSource.SelectedIndex < 0)
            {
                Balloon.balloon("请选择收文来源", cmbSource);
                return false;
            }
            if (chkChangeStatus.Checked && cmbCaseStatus.SelectedIndex < 0)
            {
                Balloon.balloon("请选择新案件的状态", cmbCaseStatus);
                return false;
            }
            if (!radioByRecvDate.Checked && !radioBySendDate.Checked)
            {
                Balloon.balloon("必需选择一种计算方式", radioByRecvDate);
                return false;
            }

            return true;
        }

        /// <summary>
        /// 用界面上的值更新收文信息
        /// </summary>
        /// <param name="rf"></param>
        public void UpdateRecvFile(RFCategoryInfo rf)
        {
            rf.RFIndex = txtRFIndex.Text;
            rf.RFName = txtRFName.Text.Trim();
            rf.AddDay = txtAddDay.Value;
            rf.AddMon = txtAddMonth.Value;
            rf.DateType = radioByRecvDate.Checked ? 0 : 1;
            rf.isChangeCaseStatus = chkChangeStatus.Checked;
            rf.isNeedAnswer = chkIsNeedAnswer.Checked;
            rf.isNeedApplyInfo = chkIsNeedAnswer.Checked;
            rf.Source = cmbSource.GetSelectedSubKey();
            if (rf.isChangeCaseStatus)
            {
                rf.NewCaseStatus = int.Parse(cmbCaseStatus.SelectedDictKey);
            }
        }
    }
}
